﻿//Creada por Laura 28/06/10. Nuestro sniper es una mira que va a poder disparar a los
//zombies en pantalla

#region Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Engine;
#endregion

namespace Game
{
    class Sniper
    {
        #region Constants

        //La velocidad del cursos medidad en pixeles por segundo
        const float CursorSpeed = 250.0f;
       
        #endregion

        #region Fields and properties

        Texture2D mCursorTexture;
        Vector2 mTextureCenter;
        GraphicsDevice mGraphicDevice;
        PlayerIndex mSniperIndex;
        Viewport mViewPort;
        int mIndexNumber;

        // La posicion del cursor en screen space 
        private Vector2 mPosition;

        public Vector2 Position
        {
            get { return mPosition; }
        }

        #endregion

        #region Creation and initialization

        public Sniper(Texture2D texture,GraphicsDevice gDevice,PlayerIndex index)
        {
            mCursorTexture = texture;
            mGraphicDevice = gDevice;
            mSniperIndex = index;
            mIndexNumber = (int)mSniperIndex;

            mTextureCenter = new Vector2(
            mCursorTexture.Width / 2, mCursorTexture.Height / 2);

            mViewPort = mGraphicDevice.Viewport;

            // on Xbox360, initialize is overriden so that we can center the cursor once we
            // know how big the viewport will be.
//#if XBOX360
            
            mPosition.X = mViewPort.X + (mViewPort.Width / 2);
            mPosition.Y = mViewPort.Y + (mViewPort.Height / 2);
//#endif

        }

        #endregion

        #region Draw

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(mCursorTexture, Position, null, Color.White, 0.0f,
                mTextureCenter, 1.0f, SpriteEffects.None, 0.0f);
        }

        #endregion

        #region Update

        // Update gets the current gamepad state and mouse state and uses that data to
        // calculate where the cursor's position is on the screen. On xbox, the position
        // is clamped to the viewport so that the cursor can't go off the screen. On
        // windows, doing something like that would be rude :)
        public void Update(GameTime gameTime)
        {
            if (gameTime == null)
                throw new ArgumentNullException("gameTime");

            GamePadState currentState = Input.GetGamePad(mIndexNumber);

            // we'll create a vector2, called delta, which will store how much the
            // cursor position should change.
            Vector2 delta = currentState.ThumbSticks.Left;

            // down on the thumbstick is -1. however, in screen coordinates, values
            // increase as they go down the screen. so, we have to flip the sign of the
            // y component of delta.
            delta.Y *= -1;

            // check the dpad: if any of its buttons are pressed, that will change delta
            // as well.
            if (currentState.DPad.Up == ButtonState.Pressed)
            {
                delta.Y = -1;
            }
            if (currentState.DPad.Down == ButtonState.Pressed)
            {
                delta.Y = 1;
            }
            if (currentState.DPad.Left == ButtonState.Pressed)
            {
                delta.X = -1;
            }
            if (currentState.DPad.Right == ButtonState.Pressed)
            {
                delta.X = 1;
            }

            // normalize delta so that we know the cursor can't move faster than
            // CursorSpeed.
            if (delta != Vector2.Zero)
            {
                delta.Normalize();
            }

//#if XBOX360
        // modify position using delta, the CursorSpeed constant defined above, and
        // the elapsed game time.
            mPosition += delta * CursorSpeed *
            (float)gameTime.ElapsedGameTime.TotalSeconds;

        // clamp the cursor position to the viewport, so that it can't move off the
        // screen.

            mPosition.X = MathHelper.Clamp(mPosition.X, mViewPort.X, mViewPort.X + mViewPort.Width);
            mPosition.Y = MathHelper.Clamp(mPosition.Y, mViewPort.Y, mViewPort.Y + mViewPort.Height);
//#else

//            mPosition.X = Input.CurrentMouseState.X;
//            mPosition.Y = Input.CurrentMouseState.Y;

            
//            //// modify position using delta, the CursorSpeed constant defined above,
//            //// and the elapsed game time, only if the cursor is on the screen

//            if ((mViewPort.X <= mPosition.X) && (mPosition.X <= (mViewPort.X + mViewPort.Width)) &&
//                (mViewPort.Y <= mPosition.Y) && (mPosition.Y <= (mViewPort.Y + mViewPort.Height)))
//            {
//                mPosition += delta * CursorSpeed *
//                    (float)gameTime.ElapsedGameTime.TotalSeconds;
//                mPosition.X = MathHelper.Clamp(mPosition.X, mViewPort.X, mViewPort.X + mViewPort.Width);
//                mPosition.Y = MathHelper.Clamp(mPosition.Y, mViewPort.Y, mViewPort.Y + mViewPort.Height);
//            }
//            else if (delta.LengthSquared() > 0f)
//            {
//                mPosition.X = mViewPort.X + mViewPort.Width / 2;
//                mPosition.Y = mViewPort.Y + mViewPort.Height / 2;
//            }

//            // set the new mouse position using the combination of mouse and gamepad
//            // data.
//            Mouse.SetPosition((int)mPosition.X, (int)mPosition.Y);

//#endif
            
        }

        #endregion

        // CalculateCursorRay Calculates a world space ray starting at the camera's
        // "eye" and pointing in the direction of the cursor. Viewport.Unproject is used
        // to accomplish this. see the accompanying documentation for more explanation
        // of the math behind this function.
        public Ray CalculateCursorRay(Matrix projectionMatrix, Matrix viewMatrix)
        {
            // create 2 positions in screenspace using the cursor position. 0 is as
            // close as possible to the camera, 1 is as far away as possible.
            Vector3 nearSource = new Vector3(Position, 0f);
            Vector3 farSource = new Vector3(Position, 1f);

            // use Viewport.Unproject to tell what those two screen space positions
            // would be in world space. we'll need the projection matrix and view
            // matrix, which we have saved as member variables. We also need a world
            // matrix, which can just be identity.
            Vector3 nearPoint = mGraphicDevice.Viewport.Unproject(nearSource,
                projectionMatrix, viewMatrix, Matrix.Identity);

            Vector3 farPoint = mGraphicDevice.Viewport.Unproject(farSource,
                projectionMatrix, viewMatrix, Matrix.Identity);

            // find the direction vector that goes from the nearPoint to the farPoint
            // and normalize it....
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            // and then create a new ray using nearPoint as the source.
            return new Ray(nearPoint, direction);
        }
    }
}
